package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.itemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private itemService itemservice;

    @Override
    public List<Cart> saveCart(List<Cart> cart, Long itemId, Integer num) {
        TbItem tbItem = itemservice.getItemById(itemId);
        String sellerId = tbItem.getSellerId();
        Cart cartBySellerId = getCartBySellerId(cart, sellerId);
        if(cartBySellerId == null){
            cartBySellerId = new Cart();
            cartBySellerId.setSellerId(sellerId);
            TbOrderItem tbOrderItem = creteOderItem(tbItem, num);
            List<TbOrderItem> tbOrderItems = new ArrayList<>();
            tbOrderItems.add(tbOrderItem);
            cartBySellerId.setOrderItemList(tbOrderItems);
            cart.add(cartBySellerId);
        }else{
            TbOrderItem orderItemById = getOrderItemById(cartBySellerId.getOrderItemList(), itemId);
            /*如果没有,就创建一个明细,加到购物车明细列表*/
            if(orderItemById == null){
                orderItemById = creteOderItem(tbItem,num);
                cartBySellerId.getOrderItemList().add(orderItemById);
            }else{
                /*如果有就修改小计和数量*/
                orderItemById.setNum(orderItemById.getNum() + num);
                /*总价格*/
                orderItemById.setTotalFee(new BigDecimal(orderItemById.getPrice().doubleValue() * orderItemById.getNum()));
                /*数量为零从购物车删除*/
                if(orderItemById.getNum().intValue() <= 0){
                    cartBySellerId.getOrderItemList().remove(orderItemById);
                }
                /*购物车为空,移除购物车*/
                if(cartBySellerId.getOrderItemList().size() <= 0){
                    cart.remove(cartBySellerId);
                }
            }
        }
        return cart;
    }

    @Override
    public List<Cart> getCarListFormRedis(String username) {
        List<Cart> cartList = (List<Cart>)redisTemplate.boundHashOps("cartList").get(username);
        if(cartList == null){
            cartList = new ArrayList<>();
        }
        return cartList;
    }

    @Override
    public void saveCartListToRedis(List<Cart> list, String username) {
        redisTemplate.boundHashOps("cartList").put(username,list);
    }

    /*合并*/
    @Override
    public List<Cart> mergeCartList(List<Cart> redislist, List<Cart> cookielist) {
        for(Cart cart : cookielist){
            for (TbOrderItem tbOrderItem : cart.getOrderItemList()){
                redislist = saveCart(redislist,tbOrderItem.getItemId(),tbOrderItem.getNum());
            }
        }
        return redislist;
    }

    private Cart getCartBySellerId(List<Cart> cartList,String sellerId){
        for(Cart cart :cartList){
            if(Objects.equals(cart.getSellerId(),sellerId)){
                return cart;
            }
        }
        return null;
    }
    private TbOrderItem getOrderItemById(List<TbOrderItem> tbOrderItems,Long itemId){
        for (TbOrderItem tbOrderItem : tbOrderItems){
            if(Objects.equals(tbOrderItem.getItemId(),itemId)){
                return tbOrderItem;
            }
        }
        return null;
    }
    //封装
    private TbOrderItem creteOderItem(TbItem tbItem,Integer num){
        TbOrderItem tbOrderItem = new TbOrderItem();
        tbOrderItem.setSellerId(tbItem.getSellerId());
        tbOrderItem.setNum(num);
        tbOrderItem.setItemId(tbItem.getId());
        tbOrderItem.setGoodsId(tbItem.getGoodsId());
        tbOrderItem.setTitle(tbItem.getTitle());
        tbOrderItem.setPrice(tbItem.getPrice());
        tbOrderItem.setPicPath(tbItem.getImage());
        tbOrderItem.setTotalFee(new BigDecimal(num * tbItem.getPrice().doubleValue()));
        return tbOrderItem;
    }
}
